<?php

namespace App\Http\Controllers\Admin;
use \Hash;
use \DB;
use Illuminate\Http\Request;
use App\Http\Requests\GoodInsertRequest;
use App\Http\Requests\GoodUpdateRequest;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class GoodController extends Controller
{

	//获取主页
		public function getIndex(Request $request)
		{

		$lists['num']= $request->input('num',3);
		$lists['keyword']= $request->input('keyword','');
		$lists['page']= $request->input('page',1);
		$request->session()->put('num',$lists['num']);
		$request->session()->put('keyword',$lists['keyword']);
		$request->session()->put('page',$lists['page']);
		//进行判断
			if($lists['keyword'])
			{
			//进行搜索操作
				//获取所有的类别数组
					$types = DB::table('shop_type')->get();
					
					$arr = [];
					foreach ($types as $k => $v) {
						$arr[$v->tid]=$v->tname;
					} 
				//判断给定的值是否存在数组中返回相应的健名
					$key = array_search($lists['keyword'],$arr);
					// dd($arr);
				//判断$key是否有值
					if($key)
					{
						//链接数据库进行查询 两表联查
							// $goods = DB::table('shop_goods')      
				   //          ->leftJoin('shop_type', 'shop_goods.tid', '=', 'shop_type.tid')->select(DB::raw('shop_goods.*,tname,path,concat(path,shop_goods.tid) as paths'))
				   //          ->where('paths','like','%,'.$key.',%')->paginate($lists['num']);
			            //链接数据库 有问题 当男装下面有 商品裤子 和 类别男下衣的时候 查询男装这个类别,男装下的类别男下衣的产品会显示出来,但是男装下的商品 裤子显示不出来
						//链接数据库进行查询 两表联查
							$goods = DB::table('shop_goods')      
				            ->leftJoin('shop_type', 'shop_goods.tid', '=', 'shop_type.tid')
				            ->select(DB::raw('shop_goods.*,tname,path'))
				            ->where('path','like','%,'.$key.',%')->paginate($lists['num']);
				        	// dd($goods->total());
				        //第二个问题 $goods 是一个 对象 不是一个带数组的对象?
				        
					}else{
						//链接数据库进行查询 两表联查
						$goods = DB::table('shop_goods')
			            ->select(DB::raw('shop_goods.*,tname'))->leftJoin('shop_type', 'shop_goods.tid', '=', 'shop_type.tid')
			            ->where('gname','like','%'.$lists['keyword'].'%')->paginate($lists['num']);
						// dd($goods);
						//未查找进行判断
				            
					}



				
			}else{

					//链接数据库进行查询 两表联查
				$goods = DB::table('shop_goods')
	            ->select(DB::raw('shop_goods.*,tname'))->leftJoin('shop_type', 'shop_goods.tid', '=', 'shop_type.tid')
	            ->paginate($lists['num']);
	            // dd($goods);
			}
		 
			
            // dd($goods);
			// $goods = DB::table('shop_goods')->get();
			$sta = [0=>'新品',1=>'上架',2=>'下架',3=>'特殊'];
			             if(!$goods->total())
				            {
				            	return back()->with('error','您未查到任何信息');
				            }
		   return view('admin.good.index',['goods'=>$goods,'sta'=>$sta,'lists'=>$lists]);
			
		}
	//商品描述页
		public function getGdesc(Request $request)
		{
			$num = $request->session()->get('num');
		    $keyword = $request->session()->get('keyword');
		    $page=$request->session()->get('page');
			//获取id
			$id = $request->input('id');
			//链接数据库进行查询
			$good = DB::table('shop_goods')->select(DB::raw('shop_goods.*,tname'))->leftJoin('shop_type','shop_goods.tid','=','shop_type.tid')
			->where('gid',$id)->first();
			// dd($good);
			// $good = DB::table('shop_goods')->where('gid',$id)->first();
			$sta = [0=>'新品',1=>'上架',2=>'下架',3=>'特殊'];
			return view('admin.good.gdesc',['good'=>$good,'sta'=>$sta,'num'=>$num,'keyword'=>$keyword,'page'=>$page]);

		}
	//进行上架下架处理
		public function getStatus(Request $request)
		{
			$value = $request->input('sta');
			$gid = $request->input('gid');
			

			//链接数据库执行修改操作
			$res = DB::table('shop_goods')->where('gid',$gid)->update(['status'=>$value]);
			echo $res;
		}


	//添加操作
		public function getAdd()
		{
			//获取所有的类
			// $cates = DB::table('shop_type')->get();
			//连接数据库查询所有数据
	  			$cates = DB::select('select *,concat(path,tid) as paths from shop_type order by paths');

			//进行遍历
			foreach ($cates as $k => $v)
			 {
			 	$str = rtrim($v->path,',');
	  			$arr = explode(',', $str);
			 	$len = count($arr)-1;
			 	$v->tname = str_repeat('---|',$len).$v->tname;
			 } 
			 return view('admin.good.add',['cates'=>$cates]);
		}
	//处理添加的数据
		public function postDoadd(Request $request)
		{

			$data = $request->only(['tid','gname','price','cnt','status','gdesc']);
			$data['ctime']=time();
			//进行图片处理
			// dd($data);
			if($request->hasFile('gpic'))
			{
				//获取文件的拓展名
				$ex = $request->file('gpic')->getClientOriginalExtension();
				//拼接新的文件名
				$newname = md5(time().rand(1,9999));
				//进行移动
				$request->file('gpic')->move('./upload/good/',$newname.'.'.$ex);
				$data['gpic']='/upload/good/'.$newname.'.'.$ex;
			}else{
				return back()->with('error','图片未上传');
			}
				// dd($data);
				//链接数据库进行插入操作
				$res=DB::table('shop_goods')->insert($data);
				
			//每页显示
			// $request->session()->put('num',3);
			$page = $request->session()->get('num',function(){
				return 3;
			});
			// dd($page);
			//获取总条数
			$to = DB::table('shop_goods')->paginate($page);
			//获取最大页
			$maxpage=$to->lastPage();
			// dd($maxpage);

			if($res)
			{

				return redirect('/admin/good/index?page='.$maxpage.'&num='.$page)->with('success','商品添加成功');
			}else{
				
				return back()->with('error','商品添加失败');
			}

		}

	//执行删除操作
		public function postDelete(Request $request)
		{
			$id = $request->input('gid');
			//执行删除操作
			$res = DB::table('shop_goods')->where('gid',$id)->delete();
			// $request->session()->flush();
			echo $res;
		}
	//执行修改操作
		public function getEdit(Request $request)
		{

			

			$gid = $request->input('gid');
			$btn = $request->input('btn','');
			$request->session()->put('gid',$gid);
			$request->session()->put('btn',$btn);
			//链接数据库进行查找
			$good = DB::table('shop_goods')->where('gid',$gid)->first();
			//获取所有的类别
			$cates = DB::select('select *,concat(path,tid) as paths from shop_type order by paths');
			//路径进行操作
			foreach ($cates as $k => $v) 
			{
				$str = rtrim($v->path,',');
				$arr = explode(',',$str);
				$len = count($arr)-1;
				$v->tname = str_repeat('---|',$len).$v->tname;
			}
			//解析页面
			return view('/admin/good/edit',['good'=>$good,'cates'=>$cates]);
		}
	//处理修改的数据
		public function postDoedit(Request $request)
		{
			
			$num = $request->session()->get('num');
		    $keyword = $request->session()->get('keyword');
		    $page=$request->session()->get('page');
		    $btn=$request->session()->get('btn');

			$data = $request->only(['gname','price','cnt','status','gdesc','tid']);
			$id = $request->session()->get('gid');
			// dd($request->all());
			//进行图片处理
			if($request->hasFile('gpic'))
			{

				$ex = $request->file('gpic')->getClientOriginalExtension();
				$newname = md5(time().rand(1,9999));
				$request->file('gpic')->move('./upload/good/',$newname.'.'.$ex);
				$data['gpic'] = '/upload/good/'.$newname.'.'.$ex;
			}
			//执行修改
	    		$res=DB::table('shop_goods')->where('gid',$id)->update($data);
	    		if($res)
	    		{
	    			if($btn=='gdesc')
	    			{
	    			  return redirect('/admin/good/gdesc?id='.$id)->with('success','商品修改成功');

	    			}
	    			  return redirect('/admin/good/index?num='.$num.'&keyword='.$keyword.'&page='.$page)->with('success','商品修改成功');
	    		}else{
	    			return back()->with('error','商品修改失败');

	    		}
				
			

			
		}

}
